/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package com.xiashitech.agent.instrumentation.netty.netty_v4_common;

//import com.google.auto.value.AutoValue;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpRequest;

import javax.annotation.Nullable;
import java.net.SocketAddress;

//@AutoValue
public abstract class HttpRequestAndChannel {

  public HttpRequestAndChannel(HttpRequest request, Channel channel, SocketAddress socketAddress) {
  }

  public static HttpRequestAndChannel create(HttpRequest request, Channel channel) {
    return new HttpRequestAndChannel(request, channel, channel.remoteAddress()) {
      @Override
      public HttpRequest request() {
        return request;
      }

      @Override
      public Channel channel() {
        return channel;
      }

      @org.jetbrains.annotations.Nullable
      @Override
      public SocketAddress remoteAddress() {
        return null;
      }
    };
  }

  public abstract HttpRequest request();

  public abstract Channel channel();

  // we're capturing the remote address early because in case of timeouts or other connection issues
  // netty may return null when calling Channel.remoteAddress() at the end of processing
  @Nullable
  public abstract SocketAddress remoteAddress();
}
